<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="店铺名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入店铺名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="负责人" prop="price">
        <el-input
          v-model="queryParams.lname"
          placeholder="负责人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商家类型" prop="address">
        <el-select
          v-model="queryParams.type"
          placeholder="Select"
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="item in options"
            :key="item.fId"
            :label="item.fName"
            :value="item.fId"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:store:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:store:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:store:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:store:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="storeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="申请ID" align="center" prop="id"/>
      <el-table-column label="审核状态" align="center" prop="status" #default="scan">
        <span v-if="scan.row.status == 0">已处理</span>
        <span v-if="scan.row.status == 1">
          <a style="color: red">
            资料有误
          </a>
        </span>
        <span v-if="scan.row.status == 2">等待处理</span>
      </el-table-column>
      <el-table-column label="店铺名称" align="center" prop="name" />
      <el-table-column label="店铺类型" align="center" prop="typeName" />
      <el-table-column label="负责人" align="center" prop="leader.name" #default="sName">
        <span v-if="sName.row.leader!=null">
          {{sName.row.leader.name}}
        </span>
        <span v-if="sName.row.leader==null">
          没有
        </span>
      </el-table-column>
      <el-table-column label="联系电话" align="center" prop="leader.phone" width="110"/>
      <el-table-column label="身份证" align="center" prop="leader.idcard" width="150"/>
      <el-table-column label="所属拼团点" align="center" prop="community.title" #default="sTitle" width="110">
        <span v-if="sTitle.row.community!=null">
          <a style="color: #00afff" @click="seTitle(sTitle.row.community.title)">
            {{sTitle.row.community.title}}
          </a>
        </span>
        <span v-if="sTitle.row.community==null">
          没有
        </span>
      </el-table-column>
      <el-table-column label="社会信用代码" align="center" prop="leader.invitationCode" #default="incit">
<!--        <span v-if="incit.row.leader!=null && incit.row.status==2 || incit.row.status==1">-->
<!--          {{incit.row.leader.invitationCode}}-->
<!--        </span>-->
        <span v-if="incit.row.leader!=null && incit.row.status==1">
          <a style="color: red">
            {{incit.row.leader.invitationCode}}
          </a>
        </span>

        <span v-if="incit.row.leader!=null && incit.row.status==2">
          {{incit.row.leader.invitationCode}}
        </span>

      </el-table-column>
      <el-table-column label="门店地址" align="center" prop="community.address" #default="addrs" width="150">
        <span v-if="addrs.row.community!=null && addrs.row.status==1">
          <a style="color: red">
            {{addrs.row.community.address}}
          </a>
        </span>
<!--        <span v-if="addrs.row.community!=null && addrs.row.status==0">-->
<!--            {{addrs.row.community.address}}-->
<!--        </span>-->
        <span v-if="addrs.row.community!=null && addrs.row.status==2">
            {{addrs.row.community.address}}
        </span>
        <span v-if="addrs.row.community==null">
          没有
        </span>

      </el-table-column>
      <el-table-column label="证件图片" align="center" #default="sImage">
        <a @click="ABC(sImage.row.id)" style="color: #00afff">
          点击查看
        </a>
      </el-table-column>
      <el-table-column label="申请时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope" width="200">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:store:edit']"
          >通过</el-button>|
          |<el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:store:remove']"
          >不通过</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="店铺名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入店铺名称" />
        </el-form-item>
        <el-form-item label="商铺总金额" prop="price">
          <el-input v-model="form.price" placeholder="请输入商铺总金额" />
        </el-form-item>
        <el-form-item label="店铺地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入店铺地址" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="title" :visible.sync="openA" width="500px" append-to-body>
      <el-form ref="form" :model="formA" :rules="rules" label-width="80px">
        <img :src="formA.foodHygieneApproval" alt="" style="width: 200px;height: 200px">
        <img :src="formA.frontIdCard" alt="" style="width: 200px;height: 200px">
        <img :src="formA.license" alt="" style="width: 200px;height: 200px">
        <img :src="formA.sideIdCard" alt="" style="width: 200px;height: 200px">
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {listImage, listStoreLeader, updateStoreVo, UpdateStoreVoNo} from "@/api/system/storeVo";
import {Selectclass} from "@/api/system/store";

export default {
  name: "Store",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 管理表格数据
      storeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openA: false,
      options:{},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        type: null,
        status: null,
        lname:null
      },
      // 表单参数
      form: {},
      formA: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.clas();
  },
  methods: {
    /** 查询管理列表 */
    getList() {
      this.loading = true;
      listStoreLeader(this.queryParams).then(response => {
        var reg = /^(\d{4})\d+(\d{4})$/;
        response.data.list.forEach(item => {
          item.leader.idcard = item.leader.idcard.replace(reg, '$1********$2');
        });
        this.storeList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        type: null,
        status: null,
        price: null,
        createTime: null,
        updateTime: null,
        address: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作,刷新页面 */
    resetQuery() {
      location.reload();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      console.log(row)
      this.reset();
      updateStoreVo(row).then(response => {
        if(response.flag==true){
          this.getList()
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateStore(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStore(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    clas(){
      Selectclass().then(res=>{
        this.options=res.data
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      UpdateStoreVoNo(row).then(res=>{
        if(res.flag==true){
          this.getList();
        }
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/store/export', {
        ...this.queryParams
      }, `store_${new Date().getTime()}.xlsx`)
    },
    ABC(row){
      console.log(row)
      listImage(row).then(res=>{
        if(res.flag==true){
          this.openA=true;
          this.formA=res.data;
        }
      })
    },
    seTitle(row){
      this.$router.push({
        name:"Community",
        params:{title:row} //这里的params是一个对象，id是属性名，item.id是值(可以从当前组件或者Vue实例上直接取)
      })
    }
  }
};
</script>
